package cn.gene.timeyan.article.webapi.controller;


import cn.gene.timeyan.article.service.ArticleService;
import cn.gene.timeyan.common.restful.JsonPage;
import cn.gene.timeyan.common.restful.JsonResult;
import cn.gene.timeyan.pojo.article.dto.ArticleDTO;
import cn.gene.timeyan.pojo.article.dto.NewArticleDTO;
import cn.gene.timeyan.pojo.article.model.Article;
import cn.gene.timeyan.pojo.article.vo.ArticleVO;
import cn.gene.timeyan.pojo.article.vo.SimpleArticleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 鏂囩珷淇℃伅 前端控制器
 * </p>
 *
 * @author LWG
 * @since 2022-07-25
 */
@RestController
@RequestMapping("/article")
@Api(tags = "查询分享模块所有信息")
public class ArticleController {
    @Autowired(required = false)
    private ArticleService articleService;

    // 搜索模块最主要的功能就是实现搜索
    // 面对这种功能对应的控制器方法,可以不写任何路径
    // 按下面@GetMapping注解表示当前控制路径为localhost:10008/search
    @GetMapping("/share")
    @ApiOperation("根据关键字查询ES中的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码", name = "pageNum", dataType = "int",
                    required = true),
            @ApiImplicitParam(value = "每页条数", name = "pageSize", dataType = "int",
                    required = true)
    })
    public JsonResult<JsonPage<Article>> getShareByPage(

            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "2") Integer pageSize
    ) {
        JsonPage<Article> list = articleService.getShareByPage(pageNum, pageSize);
        return JsonResult.ok(list);

    }
    @GetMapping("/{modelId}/list")
    @ApiOperation("文章列表")
    public JsonResult<List<SimpleArticleVO>> list(@PathVariable("modelId") Long modelId){
        List<SimpleArticleVO> list=articleService.getArticleList(modelId);
        return JsonResult.ok(list);
    }

    @GetMapping("/{modelId}/list/{page}")
    @ApiOperation("文章列表再加载")
    public JsonResult<List<SimpleArticleVO>> listNext( @PathVariable("modelId") Long modelId,@PathVariable("page") Integer page){
        List<SimpleArticleVO> list=articleService.getArticleListNext(modelId,page);
        return JsonResult.ok(list);
    }

    @RequestMapping("/detail")
    @ApiOperation("文章详情显示")
    public JsonResult<ArticleVO> articleDetail(@RequestBody ArticleDTO articleDTO){
        ArticleVO articleVO=articleService.articleDetail(articleDTO);
        return JsonResult.ok(articleVO);
    }
    @PutMapping("/display/{id}")
    @ApiOperation("显示文章")
    public JsonResult<Void> display(@PathVariable("id") String id){
        articleService.display(id);
        return JsonResult.ok();
    }
    @PutMapping("/hide/{id}")
    @ApiOperation("隐藏文章")
    public JsonResult<Void> hide(@PathVariable("id") String id){
        articleService.hide(id);
        return JsonResult.ok();
    }

    @GetMapping("/list/{userId}/{page}")
    @ApiOperation("获取用户发表的分享列表")
    public JsonResult<List<SimpleArticleVO>> userArticleList(@PathVariable(value = "userId")Long userId,@PathVariable(value = "page")Integer page){
        return articleService.getUserArticleList(userId,page);
    }

    @PostMapping("/add-new")
    @ApiOperation("添加新分享")
    public JsonResult<Void> addNewArticle(@RequestBody NewArticleDTO newArticleDTO){
        return articleService.addNewArticle(newArticleDTO);
    }
}

